package com.couldsail.takeaway.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Data
@Slf4j
@AllArgsConstructor
@ConfigurationProperties(prefix = "local.gz.utils")
public class LocationUtil {
    private String url;
    private String directory;
    private String imageUrl;

    public String upload(MultipartFile file, String name) throws IOException {
        // 检查并创建上传目录
        File dir = new File(directory);
        if (!dir.exists()) {
            // 目录不存在则创建目录
            dir.mkdirs();
        }

        // 将文件保存到指定位置
        File dest = new File(directory + name);
        // 移动目录
        file.transferTo(dest);

        return imageUrl + name;
    }
}
